<?php

namespace backend\components;

use Yii;
use yii\base\Component;

/**
 * Class SidebarMenuProvider
 *
 * 提供导航菜单数据的组件
 * 'sidebarMenuProvider' => [
 *       'class' => 'admin\components\view\SidebarMenuProvider',
 *       'itemDataPath'=>'@backend/config/menu/menu.php'
 * ],
 *
 * @package admin\components\view
 */
class SidebarMenuProvider extends Component
{

    /**
     * @var array
     */
    public $items = [];

    /**
     * @var
     */
    public $itemDataPath;

    /**
     * @var string
     */
    public $menuTranslateCategory = 'backend';

    public function t($string, $category = null)
    {
        $category = $category == null ? $this->menuTranslateCategory : $category;
        return Yii::t($category, $string);
    }

    public function getItems()
    {
        if (empty($this->items))
        {
            $this->initItems();
        }
        return $this->items;
    }

    /**
     * 初始化菜单数据
     *
     * @return array|mixed
     */
    public function initItems()
    {
        $items = [];
        $realPath = Yii::getAlias($this->itemDataPath);
        Yii::trace("load admin menu data file:" . $realPath, __METHOD__);
        if (file_exists($realPath))
        {
            $items = require $realPath;
        }
        else
        {
            Yii::error("admin menu file:" . $realPath . " not exist.");
        }

        $this->filterItems($items);
        $this->items = $items;

        return $items;
    }

    /**
     * 过滤菜单
     *
     * @param      $items
     * @param null $parent
     */
    public function filterItems(& $items, & $parent = null)
    {
        foreach ($items as $key => & $item)
        {
            if (isset($item['items']))
            {
                $this->filterItems($item['items'], $item);
                if (empty($item['items']))
                {
                    $item['visible'] = false;
                }
            }
            else
            {
                if (isset($item['visible']))
                {
//                    if ( ! $item['visible'])
//                    {
//                        unset($items[$key]);
//                    }
                }
                else
                {
                    if ($this->hasPermissionToAccessItem($item))
                    {
                        if ($parent)
                        {
                            $parent['visible'] = true;
                        }
                        $item['visible'] = true;
                    }
                    else
                    {
                        unset($items[$key]);
                    }
                }
            }
        }
    }

    /**
     * 是否拥有权限接入
     *
     * @param $item
     * @return bool
     */
    public function hasPermissionToAccessItem($item)
    {
        if (isset($item['auth']))
        {
            $isShowItem = false;
            foreach ($item['auth'] as $authPermissionName)
            {
                if (Yii::$app->getUser()->can($authPermissionName))
                {
                    $isShowItem = true;
                    break;
                }
            }
            if (false == $isShowItem)
            {
                return false;
            }
        }
        else
        {
            return false;
        }
        return true;
    }
}
